package com.suli.multimedia.service.serviceImpl;

import com.suli.multimedia.entity.Hotel;
import com.suli.multimedia.mapper.HotelMapper;
import com.suli.multimedia.service.HotelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suli.multimedia.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuLi
 * @since 2022-04-18
 */
@Service
public class HotelServiceImpl extends ServiceImpl<HotelMapper, Hotel> implements HotelService {

    @Autowired
    HotelMapper hotelMapper;
    @Autowired
    OssUtil ossUtil;

    @Override
    public Map<String, Object> getHotelList() {
        List<Hotel> hotels = hotelMapper.selectList(null);
        Map<String, Object> map=new HashMap<>();
        map.put("total", hotels.size());
        map.put("hotelList", hotels);
        return map;
    }

    @Override
    public boolean deleteHotel(Integer hotelId) {
        Hotel hotel = hotelMapper.selectById(hotelId);
        if(ObjectUtils.isEmpty(hotel)){
            return false;
        }
        int i = hotelMapper.deleteById(hotelId);
        ossUtil.delete(hotel.getHotelImage().substring(42));
        return i>0;
    }

    @Override
    public boolean deleteImage(Integer hotelId) {
        Hotel hotel = hotelMapper.selectById(hotelId);
        if(ObjectUtils.isEmpty(hotel)){
            return false;
        }
        String image=hotel.getHotelImage().substring(42);
        ossUtil.delete(image);
        hotel.setHotelImage("");
        hotelMapper.updateById(hotel);
        return true;
    }
}
